<template>
  <div class="login-container">
    <!-- 主题切换按钮 -->
    <div class="theme-toggle-btn">
      <ThemeToggle />
    </div>
    
    <!-- 左侧品牌展示 -->
    <div class="login-left">
      <div class="brand-section">
        <div class="brand-logo">
          <div class="logo-icon">💧</div>
        </div>
        <h1 class="brand-title">二次供水智能体</h1>
        <p class="brand-subtitle">智慧水务 · 安全供水 · 精准管控</p>
      </div>
      <div class="feature-list">
        <div class="feature-item">
          <span class="feature-icon">💬</span>
          <span>自然语言查询运行数据</span>
        </div>
        <div class="feature-item">
          <span class="feature-icon">📊</span>
          <span>动态生成图表与报表</span>
        </div>
        <div class="feature-item">
          <span class="feature-icon">🔍</span>
          <span>关联设备台账与知识库</span>
        </div>
        <div class="feature-item">
          <span class="feature-icon">🎯</span>
          <span>智能推荐处置方案</span>
        </div>
      </div>
    </div>

    <!-- 右侧登录表单 -->
    <div class="login-right">
      <div class="login-card">
        <div class="login-header">
          <h2>登录系统</h2>
          <p>欢迎使用二次供水智能管理平台</p>
        </div>

        <el-form ref="loginFormRef" :model="loginForm" :rules="loginRules" class="login-form">
          <el-form-item prop="username">
            <el-input 
              v-model="loginForm.username" 
              type="text" 
              auto-complete="off" 
              placeholder="请输入账号"
              clearable
              @keyup.enter="handleLogin"
            >
              <template #prefix>
                <el-icon><User /></el-icon>
              </template>
            </el-input>
          </el-form-item>

          <el-form-item prop="password">
            <el-input 
              v-model="loginForm.password" 
              type="password" 
              auto-complete="off" 
              placeholder="请输入密码"
              show-password
              @keyup.enter="handleLogin"
            >
              <template #prefix>
                <el-icon><Lock /></el-icon>
              </template>
            </el-input>
          </el-form-item>

          <el-form-item prop="code" v-if="captchaOnOff">
            <div class="captcha-wrapper">
              <el-input 
                v-model="loginForm.code" 
                type="text" 
                auto-complete="off" 
                placeholder="请输入验证码"
                @keyup.enter="handleLogin"
                class="captcha-input"
              >
                <template #prefix>
                  <el-icon><Key /></el-icon>
                </template>
              </el-input>
              <div class="captcha-image" @click="getCode">
                <img :src="codeUrl" alt="验证码" />
              </div>
            </div>
          </el-form-item>

          <el-button 
            :loading="loading" 
            type="primary"
            class="login-btn"
            @click="handleLogin"
          >
            <span v-if="!loading">登 录</span>
            <span v-else>登 录 中...</span>
          </el-button>
        </el-form>
        <div class="temp-admin-section">
          <el-button
            type="warning"
            plain
            class="temp-admin-btn"
            @click="handleTempAdmin"
          >
            临时管理员（跳过登录）
          </el-button>
        </div>

        <div class="login-footer">
          <p>© 2025 二次供水智能管理平台. 保留所有权利</p>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { login, getCodeImg } from '@/api/login'
import { setToken } from '@/utils/auth'
import { User, Lock, Key } from '@element-plus/icons-vue'
import ThemeToggle from '@/components/ThemeToggle.vue'

export default {
  name: 'LoginPage',
  components: {
    User,
    Lock,
    Key,
    ThemeToggle
  },
  data() {
    return {
      codeUrl: '',
      loginForm: {
        username: '',
        password: '',
        code: '',
        uuid: ''
      },
      loginRules: {
        username: [
          {
            required: true,
            trigger: 'blur',
            message: '请输入您的账号'
          }
        ],
        password: [
          {
            required: true,
            trigger: 'blur',
            message: '请输入您的密码'
          }
        ],
        code: [
          {
            required: true,
            trigger: 'change',
            message: '请输入验证码'
          }
        ]
      },
      loading: false,
      // 验证码开关
      captchaOnOff: true
    }
  },
  created() {
    this.getCode()
  },
  methods: {
    getCode() {
      getCodeImg().then((res) => {
        this.captchaOnOff = res.captchaOnOff === undefined ? true : res.captchaOnOff
        if (this.captchaOnOff) {
          this.codeUrl = 'data:image/gif;base64,' + res.img
          this.loginForm.uuid = res.uuid
        }
      }).catch(() => {
        // 如果获取验证码失败，关闭验证码功能
        this.captchaOnOff = false
      })
    },
    handleLogin() {
      this.$refs.loginFormRef.validate((valid) => {
        if (valid) {
          this.loading = true
          login(this.loginForm.username, this.loginForm.password, this.loginForm.code, this.loginForm.uuid)
            .then((res) => {
              // 保存token
              setToken(res.token)
              this.$router.push({
                path: this.$route.query.redirect || '/'
              }).catch(() => { })
            })
            .catch(() => {
              this.loading = false
              if (this.captchaOnOff) {
                this.getCode()
              }
            })
        }
      })
    },
    handleTempAdmin() {
      // 设置临时管理员 token
      setToken('temp-admin-token')
      this.$router.push({
        path: this.$route.query.redirect || '/'
      }).catch(() => { })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

.login-container {
  display: flex;
  min-height: 100vh;
  background: linear-gradient(135deg, #0ea5e9 0%, #1e40af 100%);
  font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
  position: relative;
}

html.dark .login-container {
  background: linear-gradient(135deg, #0c4a6e 0%, #1e3a8a 100%);
}

.theme-toggle-btn {
  position: absolute;
  top: 20px;
  right: 20px;
  z-index: 1000;
}

// 左侧品牌区域
.login-left {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  color: white;
  padding: 40px;
  position: relative;

  &::after {
    content: '';
    position: absolute;
    right: 0;
    top: 0;
    bottom: 0;
    width: 1px;
    background: rgba(255, 255, 255, 0.1);
    opacity: 0.3;
  }
}

.brand-section {
  text-align: center;
  margin-bottom: 80px;
}

.brand-logo {
  margin-bottom: 30px;
}

.logo-icon {
  width: 100px;
  height: 100px;
  background: rgba(255, 255, 255, 0.15);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 50px;
  font-weight: bold;
  margin: 0 auto;
  backdrop-filter: blur(10px);
  border: 3px solid rgba(255, 255, 255, 0.3);
  transition: all 0.3s ease;
  box-shadow: 0 8px 32px rgba(14, 165, 233, 0.3);

  &:hover {
    background: rgba(255, 255, 255, 0.25);
    transform: translateY(-5px) scale(1.05);
    box-shadow: 0 12px 40px rgba(14, 165, 233, 0.5);
  }
}

.brand-title {
  font-size: 36px;
  font-weight: 700;
  margin-bottom: 12px;
  text-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
  letter-spacing: 2px;
}

.brand-subtitle {
  font-size: 15px;
  color: rgba(255, 255, 255, 0.85);
  letter-spacing: 3px;
  font-weight: 300;
}

.feature-list {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.feature-item {
  display: flex;
  align-items: center;
  font-size: 15px;
  color: rgba(255, 255, 255, 0.9);
  padding: 8px 0;

  .feature-icon {
    font-size: 24px;
    margin-right: 15px;
    filter: drop-shadow(0 0 8px rgba(96, 239, 255, 0.6));
  }
}

// 右侧登录区域
.login-right {
  flex: 1;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 40px;
}

.login-card {
  width: 100%;
  max-width: 420px;
  background: var(--el-bg-color);
  border-radius: 16px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
  padding: 50px 40px;
  animation: slideUp 0.6s ease-out;
  
  // 当有验证码时，增加高度
  &.has-captcha {
    padding-bottom: 50px;
  }
}

@keyframes slideUp {
  from {
    opacity: 0;
    transform: translateY(30px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.login-header {
  margin-bottom: 40px;
  text-align: center;

  h2 {
    font-size: 26px;
    font-weight: 600;
    color: var(--el-text-color-primary);
    margin-bottom: 8px;
  }

  p {
    font-size: 14px;
    color: var(--el-text-color-secondary);
  }
}

::v-deep .login-form {
  width: 100%;

  .el-form-item {
    margin-bottom: 24px;

    .el-input {
      --el-input-height: 48px;
      --el-input-border-radius: 8px;

      input {
        font-size: 14px;
        border: 1.5px solid var(--el-border-color);
        background-color: var(--el-fill-color-light);
        color: var(--el-text-color-primary);
        transition: all 0.3s ease;

        &::placeholder {
          color: var(--el-text-color-placeholder);
        }

        &:hover {
          border-color: var(--el-color-primary);
          background-color: var(--el-bg-color);
        }

        &:focus {
          border-color: var(--el-color-primary);
          background-color: var(--el-bg-color);
          box-shadow: 0 0 0 3px var(--el-color-primary-light-9);
        }
      }

      .el-input__prefix {
        display: flex;
        align-items: center;
        color: var(--el-color-primary);
      }
    }
  }

  .el-form-item.is-error input {
    border-color: var(--el-color-danger) !important;
    background-color: var(--el-color-danger-light-9);

    &:focus {
      box-shadow: 0 0 0 3px var(--el-color-danger-light-8);
    }
  }

  .el-form-item__error {
    color: var(--el-color-danger);
    font-size: 12px;
    margin-top: 6px;
  }
}

.captcha-wrapper {
  display: flex;
  gap: 12px;
  width: 100%;
  align-items: flex-start;
}

.captcha-input {
  flex: 1;
}

.captcha-image {
  flex-shrink: 0;
  width: 120px;
  height: 48px;
  cursor: pointer;
  border: 1.5px solid var(--el-border-color);
  border-radius: 8px;
  overflow: hidden;
  transition: all 0.3s ease;
  background-color: var(--el-fill-color-light);

  &:hover {
    border-color: var(--el-color-primary);
    box-shadow: 0 0 0 3px var(--el-color-primary-light-9);
  }

  img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    display: block;
  }
}

.login-btn {
  width: 100%;
  height: 48px;
  font-size: 16px;
  font-weight: 600;
  letter-spacing: 1px;
  border-radius: 8px;
  border: none;
  background: linear-gradient(135deg, #0ea5e9 0%, #1e40af 100%);
  color: white;
  cursor: pointer;
  transition: all 0.3s ease;
  box-shadow: 0 10px 25px rgba(14, 165, 233, 0.4);

  &:hover {
    transform: translateY(-2px);
    box-shadow: 0 15px 35px rgba(14, 165, 233, 0.6);
    background: linear-gradient(135deg, #0284c7 0%, #1e3a8a 100%);
  }

  &:active {
    transform: translateY(0);
  }

  &.is-loading {
    opacity: 0.8;
  }
}

.temp-admin-section {
  margin-top: 20px;
  text-align: center;
}

.temp-admin-btn {
  width: 100%;
  height: 44px;
  font-size: 14px;
  border-radius: 8px;
  border: 1.5px solid var(--el-color-warning);
  color: var(--el-color-warning);
  background: transparent;
  transition: all 0.3s ease;

  &:hover {
    background: var(--el-color-warning-light-9);
    border-color: var(--el-color-warning-dark-2);
    color: var(--el-color-warning-dark-2);
  }

  &:active {
    transform: scale(0.98);
  }
}

.login-footer {
  margin-top: 30px;
  text-align: center;
  padding-top: 20px;
  border-top: 1px solid var(--el-border-color);

  p {
    font-size: 12px;
    color: var(--el-text-color-placeholder);
  }
}

// 响应式设计
@media (max-width: 1024px) {
  .login-container {
    flex-direction: column;
  }

  .login-left {
    min-height: 40vh;
    padding: 40px 20px;

    &::after {
      bottom: -1px;
      right: 0;
      width: 100%;
      height: 1px;
    }
  }

  .brand-section {
    margin-bottom: 30px;
  }

  .brand-title {
    font-size: 24px;
  }

  .feature-list {
    flex-direction: row;
    gap: 30px;
    justify-content: center;
  }

  .login-right {
    min-height: 60vh;
  }

  .login-card {
    padding: 40px 30px;
  }
}

@media (max-width: 768px) {
  .login-container {
    padding: 20px;
  }

  .login-left {
    display: none;
  }

  .login-right {
    min-height: 100vh;
  }

  .login-card {
    max-width: 100%;
    padding: 30px 20px;
  }

  .login-header h2 {
    font-size: 22px;
  }

  .login-btn {
    height: 44px;
    font-size: 14px;
  }
}
</style>


